﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class RoomNumberDAL
    {
        public RoomNumberDAL(HotelContext hotelContext)
        {
            HotelContext = hotelContext;
        }

        public HotelContext HotelContext { get; }
        /// <summary>
        /// 添加房号
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int RoomNumberAdd(RoomNumberModel info)
        {
            try
            {
                var res = HotelContext.RoomNumbers.Count(o => o.NumberName == info.NumberName);
                if (res > 0)
                {
                    return -1;//房号名称不能重复
                }
                HotelContext.RoomNumbers.Add(info);
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 房号显示分页
        /// </summary>
        /// <param name="totalCount"></param>
        /// <param name="totalPage"></param>
        /// <param name="Pageindex"></param>
        /// <param name="Pagesize"></param>
        /// <returns></returns>
        public IQueryable GetRoomNumberList(out int totalCount, out int totalPage, int Pageindex = 1, int Pagesize = 2)
        {
            try
            {
                var list = HotelContext.RoomNumbers.Join(HotelContext.roomTypeModels, a => a.RoomTypeId, b => b.Id, (a, b) => new
                {
                    b.Id,
                    b.RoomTypName,
                    b.Money,
                    b.Ansy,
                    b.Photo,
                    b.IsExtrabed,
                    b.Isbreakfast,
                    b.Number,
                    b.Desc,
                    a.IsDelete,
                    b.State,
                    b.Createtime,
                    b.Updatetime,
                    a.NumberName,
                    a.RoomeStateId,
                    a.RoomTypeId,
                    a.RoomDesc,
                    
                }).Where(o => o.IsDelete == false);
                totalCount = list.Count();
                totalPage = (int)Math.Ceiling(totalCount * 1.0 / Pagesize);
                list = list.OrderByDescending(o => o.Id).Skip((Pageindex - 1) * Pagesize).Take(Pagesize);
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 房号Id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RoomNumberModel RoomNumberId(int id)
        {
            try
            {
                return HotelContext.RoomNumbers.Find(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int RoomNumberDel(int id)
        {
            try
            {
                var res = HotelContext.RoomNumbers.Find(id);
                if (res != null)
                {
                    res.IsDelete = true;
                }
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 房型修改
        /// </summary>
        /// <param name="roomType"></param>
        /// <returns></returns>
        public int RoomNumberUpdate(RoomNumberModel info)
        {
            try
            {
                var list = HotelContext.RoomNumbers.Count(o => o.IsDelete == false && o.NumberName == info.NumberName && o.Id != info.Id);
                if (list > 0)
                {
                    return -1;
                }
                HotelContext.RoomNumbers.Update(info);
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public  int RoomNumberDelAll(string ids)
        {
            try
            {
              var idsAll=ids.Split(',');
                foreach (var id in idsAll)
                {
                    var info=HotelContext.RoomNumbers.Find(Convert.ToInt32(id));
                    info.IsDelete = true;
                }
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量上架下架
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int RoomNumberUptAll(string ids,int state)
        {
            try
            {
                var idsAll=ids.Split(',');
                foreach(var id in idsAll)
                {
                    var info = HotelContext.RoomNumbers.Find(Convert.ToInt32(id));
                    info.State= state;
                }
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}
